package com.mh.system.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Set;

//登录用户身份验证
public class LoginUserDetails implements UserDetails {

    private static final long serialVersionUID = -4731154299520078670L;

    //实体类作为属性
    private SysUser user;
    //当前用户的唯一标识,生成JWT时会用到-->我生成UUID后赋值过来的,是生成token的主体信息
    private String tokenUUID;

    //登录时间
    private Long loginTime; //新增
    //过期时间
    private Long expireTime; //新增

    //权限列表
    private Set<String> permissions; //新增

    //空参,set(),get(),重写7个方法
    public LoginUserDetails() {
    }

    public LoginUserDetails(SysUser user) {
        this.user = user;
    }

    public LoginUserDetails(SysUser user, Set<String> permissions) {
        this.user = user;
        this.permissions = permissions;
    }

    public LoginUserDetails(SysUser user, String tokenUUID) {
        this.user = user;
        this.tokenUUID = tokenUUID;
    }

    public LoginUserDetails(SysUser user, String tokenUUID, Long loginTime, Long expireTime) {
        this.user = user;
        this.tokenUUID = tokenUUID;
        this.loginTime = loginTime;
        this.expireTime = expireTime;
    }

    public SysUser getUser() {
        return user;
    }

    public void setUser(SysUser user) {
        this.user = user;
    }

    public String getTokenUUID() {
        return tokenUUID;
    }

    public void setTokenUUID(String tokenUUID) {
        this.tokenUUID = tokenUUID;
    }

    public Long getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Long loginTime) {
        this.loginTime = loginTime;
    }

    public Long getExpireTime() {
        return expireTime;
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public Set<String> getPermissions() {
        return permissions;
    }

    public void setPermissions(Set<String> permissions) {
        this.permissions = permissions;
    }

    @Override
    public String toString() {
        return "LoginUserDetails{" +
                "user=" + user +
                ", tokenUUID='" + tokenUUID + '\'' +
                ", loginTime=" + loginTime +
                ", expireTime=" + expireTime +
                ", permissions=" + permissions +
                '}';
    }

    //获取用户的权限信息-->不用Security原有的,要自定义权限校验,因此在此返回null
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    @Override
    public String getPassword() {
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        return user.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
